package com.yanqu.road.server.cmd.activity.commonteam;

import com.yanqu.road.exception.BusinessException;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.CommonTeamProto;
import com.yanqu.road.pb.worldmessage.WorldMessageProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.cmd.CrossUnionCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.activity.commonteam.CommonTeamActivity;
import com.yanqu.road.server.manager.activity.commonteam.CommonTeamMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.FixSizeLinkedList;
import com.yanqu.road.utils.YanQuMessageUtils;

@Cmd(code = Protocol.C2_COMMON_TEAM_GET_CHAT_MSG_LIST, desc = "通用组队-队伍获取聊天列表")
public class CrossCommonTeamGetChatMsgListCmd extends CrossUnionCommand {

    private long playerId;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        playerId = packet.getPlayerId();
        return true;
    }
    
    @Override
    public void execute(YanQuMessage packet) throws Exception {
        CommonTeamProto.CommonTeamGetChatListReqMsg reqMsg = CommonTeamProto.CommonTeamGetChatListReqMsg.parseFrom(packet.getBytes());
        CommonTeamProto.CommonTeamGetChatListRespMsg.Builder respMsg = CommonTeamProto.CommonTeamGetChatListRespMsg.newBuilder();
        CommonTeamActivity activity = CommonTeamMgr.getActivity(reqMsg.getActivityId(), groupId);
        if (activity == null) {
            respMsg.setRet(GameErrorCode.E_COMMON_TEAM_TEAM_ACTIVITY_NOT_EXIST);
            MessageHelper.sendPacket(serverId, playerId, YanQuMessageUtils.buildMessage(Protocol.U_COMMON_TEAM_GET_TEAM_CHAT_MSG, respMsg));
            return;
        }
        try {
            //发送聊天信息
            FixSizeLinkedList<CommonTeamProto.CommonTeamChatMsg> chatMsgList = activity.getTeamChatMsgList(packet.getPlayerId());
            respMsg.setRet(0);
            respMsg.addAllDataList(chatMsgList);
        } catch (BusinessException e) {
            respMsg.setRet(e.getCode());
        }
        //返回
        MessageHelper.sendPacket(serverId, packet.getPlayerId(), YanQuMessageUtils.buildMessage(Protocol.U_COMMON_TEAM_GET_TEAM_CHAT_MSG, respMsg));
    }

    @Override
    public void sendData() {}
}
